package com.example.zyfypt516wh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.zyfypt516wh.activities.FocusActivity;
import com.example.zyfypt516wh.activities.LoginActivity;
import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.activities.CollectActivity;
import com.example.zyfypt516wh.iface.LoginOutListener;
import com.example.zyfypt516wh.model.LoginOutModel;

public class Fragment5 extends BaseFragment {

    private TextView tvName;
    private Button btCollect;
    private Button btFollow;
    private Button btLogout;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    getActivity().finish();
                    break;
                default:
                    break;
            }
        }

    };
    private LoginOutListener loginOutListener = new LoginOutListener() {
        @Override
        public void onResponse(String msg) {
            context.startActivity(new Intent(context.getApplicationContext(), LoginActivity.class));
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //耗时操作，完成之后发送消息给Handler，完成UI更新；
                    mHandler.sendEmptyMessage(0);
                    //需要数据传递，用下面方法；
                    Message msg = new Message();
                    msg.obj = "注销成功";
                    mHandler.sendMessage(msg);
                }
            }).start();


        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override //生命周期方法， 创建 view
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment5, container, false);

    }

    @Override //生命周期方法， view 创建完成
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("---fragment5----" + getSessionID());
        tvName = view.findViewById(R.id.tv_name);
        btCollect = view.findViewById(R.id.bt_collection);
        btFollow = view.findViewById(R.id.bt_follow);
        btLogout = view.findViewById(R.id.bt_logout);
        tvName.setText(getUsername() + "欢迎你！");

        //我的收藏
        btCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CollectActivity.class);

                startActivity(intent);

            }
        });
        //我的关注
        btFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FocusActivity.class);

                startActivity(intent);
            }
        });
        //注销登录
        btLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginOutModel model = new LoginOutModel();
                model.getLoginOutResult(getSessionID(), loginOutListener);
//                getActivity().finish();
            }
        });

    }


}
